use axum::{extract::{ FromRequestParts, Path, Request, State}, middleware::Next, response::{IntoResponse, Response}};
use chat_core::User;

use crate::{AppError, AppState};


pub async fn verity_chat(State(state): State<AppState>,req: Request,next: Next) -> Response {
    let (mut parts,boby) = req.into_parts();
    let Path(chat_id) = Path::<u64>::from_request_parts(&mut parts,&state).await.unwrap();
    let user = parts.extensions.get::<User>().unwrap();
    if !state.is_chat_member(chat_id,user.id as _).await.unwrap_or_default(){
        let err = AppError::CreateMessageError(
            format!("User {} is not a member of chat {}",chat_id,user.id)
        );
        return err.into_response()
    }
    let req = Request::from_parts(parts,boby);
    next.run(req).await
}